home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / 3dkit1.zip / 3DMERGE.C < prev    next >
Text File  |  1992-05-25  |  2KB  |  85 lines

  1. /* 3dmerge  -  merge two 3d files */
  2.  
  3. /* Oscar Garcia <garciao@mof.govt.nz>, May 1992 */
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7.  
  8. #define USAGE "usage: 3dmerge infile1 [infile2 [outfile]]\n\
  9. \tIf file names are omitted, the standard i/o streams are used.\n"
  10.  
  11. #define ERROR(msg) {fputs(msg,stderr),exit(1);}
  12. #define PERROR(msg) {perror(msg),exit(1);}
  13.  
  14.  
  15. #define RECLEN 81
  16.  
  17. void main(int argc, char* argv[])
  18. {
  19.     int i, npoints1, npoints2, nlines1, nlines2, point, colour;
  20.     char record[RECLEN + 1];
  21.     FILE *input1, *input2 = stdin, *output = stdout;
  22.  
  23.     /* open files */
  24.     if (argc < 2 || argc > 4)
  25.         ERROR(USAGE);   /* wrong arguments */
  26.     input1 = fopen(argv[1], "rt");
  27.     if (input1 == NULL)
  28.     {    fputs("Can't open first input file\n", stderr);
  29.         ERROR(USAGE);
  30.     }
  31.     if (argc > 2)
  32.     {    input2 = fopen(argv[2], "rt");
  33.         if (input2 == NULL)
  34.             ERROR("Can't open second input file");
  35.     }
  36.     if (argc > 3)
  37.     {    output = fopen(argv[3], "wt");
  38.         if (output == NULL)
  39.             ERROR("Can't open output file");
  40.     }
  41.  
  42.     /* numbers of points */
  43.     fscanf(input1, "%d", &npoints1);
  44.     fscanf(input2, "%d", &npoints2);
  45.     if (ferror(input1) || ferror(input2))
  46.         PERROR("Bad input file");
  47.     fprintf(output, "%d\n", npoints1 + npoints2);
  48.  
  49.     /* copy points */
  50.     fgets(record, RECLEN, input1);        /* skip rest of line */
  51.     fgets(record, RECLEN, input2);
  52.     for (i=0; i < npoints1; i++)
  53.     {    fgets(record, RECLEN, input1);
  54.         fputs(record, output);
  55.     }
  56.     for (i=0; i < npoints2; i++)
  57.     {    fgets(record, RECLEN, input2);
  58.         fputs(record, output);
  59.     }
  60.  
  61.     /* numbers of lines */
  62.     fscanf(input1, "%d", &nlines1);
  63.     fscanf(input2, "%d", &nlines2);
  64.     fprintf(output, "%d\n", nlines1 + nlines2);
  65.  
  66.     /* copy lines */
  67.     for (i=0; i < nlines1; i++)
  68.     {    fscanf(input1, "%d %d", &point, &colour);
  69.         fprintf(output, "%d %d\n", point, colour);
  70.     }
  71.     if (feof(input1))
  72.         ERROR("Unexpected end of file in first file");
  73.     for (i=0; i < nlines2; i++)
  74.     {    fscanf(input2, "%d %d", &point, &colour);
  75.         fprintf(output, "%d %d\n", point + npoints1, colour);
  76.     }
  77.     if (feof(input2))
  78.         ERROR("Unexpected end of file in second file");
  79.  
  80.  
  81.     if (ferror(input1) || ferror(input2) || ferror(output))
  82.         PERROR("i/o error");
  83.  
  84. }
  85.